import 'package:flutter/material.dart';

class TwoFixedTop extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        // appBar: AppBar(
        //   title: const Text('两级吸顶示例'),
        // ),
        body: NestedScrollView(
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            return <Widget>[
              SliverAppBar(
                expandedHeight: 50.0,
                floating: false,
                pinned: false,
                flexibleSpace: FlexibleSpaceBar(
                  title: Text('一级吸顶'),
                  background: Container(
                    width: double.infinity,
                    height: 50,
                    color: Colors.grey,
                  ),
                  // background: Image.network(
                  //   'https://via.placeholder.com/350x150',
                  //   fit: BoxFit.cover,
                  // ),
                ),
              ),

              SliverPersistentHeader(
                delegate: _SliverPersistentHeaderDelegate(
                  child: Container(
                    color: Colors.blue,
                    child: const Center(
                      child: Text(
                        '二级吸顶',
                        style: TextStyle(color: Colors.white),
                      ),
                    ),
                  ),
                ),
                pinned: true,
              ),
            ];
          },
          body: ListView.builder(
            itemCount: 30,
            itemBuilder: (BuildContext context, int index) {
              return ListTile(
                title: Text('Item $index'),
              );
            },
          ),
        ),
      ),
    );
  }
}

class _SliverPersistentHeaderDelegate extends SliverPersistentHeaderDelegate {
  final Widget child;

  _SliverPersistentHeaderDelegate({required this.child});

  @override
  double get minExtent => 60.0;

  @override
  double get maxExtent => 60.0;

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
    return child;
  }

  @override
  bool shouldRebuild(_SliverPersistentHeaderDelegate oldDelegate) {
    return oldDelegate.child != child;
  }
}